package me.jessyan.mvparms.demo.mvp.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.xrecyclerview.XRecyclerView;
import com.example.xrecyclerview.refresh.SwipyRefreshLayout;
import com.example.xrecyclerview.refresh.SwipyRefreshLayoutDirection;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.mvparms.demo.R;

/**
 * @author DrChen
 * @Date 2019/2/26 0026.
 * qq:1414355045
 * fragment 品牌馆的Adapter
 */
public class FragBrandVpAdapter<T> extends PagerAdapter{
    private int layoutId;
    private List<T> list = new ArrayList<>();
    private LayoutInflater inflater;
    private List<Boolean> booleanList;
    private Context context;
    private FragBrandVpAdapterListener listener;

    public void setListener(FragBrandVpAdapterListener listener) {
        this.listener = listener;
    }

    public FragBrandVpAdapter(int layoutId, List<T> list, Context context) {
        this.layoutId = layoutId;
        this.list = list;
       this. context = context;
        inflater = LayoutInflater.from(context);

       booleanList = new ArrayList<>();
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(true);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view ==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = inflater.inflate(layoutId,null);
          bindView(view,booleanList);
        container.addView(view,0);
        return  view;
    }

    /**
     *
     *  列表视图
     */
    private void bindView(View view, List<Boolean> t) {
        SwipyRefreshLayout refreshLayout = view.findViewById(R.id.refresh);
        XRecyclerView recyclerView = view.findViewById(R.id.recyclerView);


        //刷新，发起网络请求
        refreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {

            }
        });



        recyclerView.setPullRefreshEnabled(false);
        recyclerView.clearHeader();

        LinearLayoutManager lr = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
        recyclerView .setLayoutManager(lr);
        ArmsUtils.configRecyclerView(recyclerView,lr);
        FragBrandRvAdapter adapter = new FragBrandRvAdapter(t);
        recyclerView.setAdapter(adapter);

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() { }

            @Override
            public void onLoadMore() {

            }
        });

        adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener<Boolean>() {
            @Override
            public void onItemClick(View view, int viewType, Boolean data, int position) {
                if(listener!=null){
                    listener.getItem(view,  data);
                }
            }
        });


    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
    }

    public  interface  FragBrandVpAdapterListener  {
        void getItem(View view,Boolean data);
    }
}
